﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class GamePlayerData : GameData< GamePlayerData >
{

	[ HideInInspector ] public SACreatureData	creatureData;
	[ HideInInspector ] public int				Stone = 0;
	[ HideInInspector ] public SAMapPosMsg		Pos;

	[ HideInInspector ] public int 				BattleMapID;

	[ HideInInspector ] public SAItemData[]		Items = new SAItemData[ GameDefine.MAX_ITEM ];

	[ HideInInspector ] public Dictionary< int , GameObject > PlayerDic = new Dictionary< int , GameObject >();
	[ HideInInspector ] public bool				Team;
	
	void Awake ()
	{
		if ( mInstance == null )
		{
			mInstance = this;

			// init here ,


		}
	}


	void updatePlayerTranform( GameObject obj )
	{
		SAPlayer player = obj.GetComponent< SAPlayer >();

		obj.transform.parent = SAMapManager.instance.creatureLayer.transform;
		obj.transform.localScale = new Vector3( 1.0f , 1.0f , 1.0f );
	}

	public GameObject createPlayer( int id )
	{
		if ( PlayerDic.ContainsKey( id ) ) 
		{
			return PlayerDic[ id ];
		}

		GameObject player = SAPlayer.CreatePlayer();

		if ( SAMapManager.instance != null && SAMapManager.instance.isLoead ) 
		{
			player.transform.parent = SAMapManager.instance.creatureLayer.transform;
			player.transform.localScale = new Vector3( 1.0f , 1.0f , 1.0f );
		}
		else
		{
			player.transform.parent = transform;
		}

		PlayerDic.Add( id , player );

		return player;
	}


	public void onLoadMap()
	{
		foreach ( KeyValuePair< int , GameObject > pair in PlayerDic )
		{
			GameObject obj = pair.Value;
			obj.transform.parent = SAMapManager.instance.creatureLayer.transform;
			obj.transform.localScale = new Vector3( 1.0f , 1.0f , 1.0f );

			SAPlayer player = obj.GetComponent< SAPlayer >();
			player.movePosReal();
		}
	}
	
	public GameObject getPlayer( int id )
	{
		if ( PlayerDic.ContainsKey( id ) ) 
		{
			return PlayerDic[ id ];
		}

		return null;
	}

	public void removePlayer( int id )
	{
		if ( PlayerDic.ContainsKey( id ) ) 
		{
			GameObject obj = PlayerDic[ id ];

			SAPlayer.DestoryPlayer( obj );

			PlayerDic.Remove( id );
		}
	}

	public void enabledPlayer( int id , bool b , int x = 0 , int y = 0 )
	{
		if ( PlayerDic.ContainsKey( id ) ) 
		{
			GameObject obj = PlayerDic[ id ];

			obj.SetActive( b );

			if ( x != 0 || y != 0 ) 
			{
				SAPlayer player = obj.GetComponent< SAPlayer >();
				player.endMoving();
				player.setPos( x , y );
			}

		}
	}

	public override void clearData()
	{
		foreach ( KeyValuePair< int , GameObject > pair in PlayerDic )
		{
			SAPlayer.DestoryPlayer( pair.Value );
		}

		PlayerDic.Clear();
	}


}





